package com.ruoyi.mall.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.mall.domain.CustomerCart;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.vo.CustomerCartVo;
import com.ruoyi.mall.domain.bo.CustomerCartBo;
import com.ruoyi.mall.service.ICustomerCartService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员购物车
 *
 * @author ruoyi
 * @date 2025-02-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/customerCart")
public class CustomerCartController extends BaseController {

    private final ICustomerCartService iCustomerCartService;

    /**
     * 查询会员购物车列表
     */
    @SaCheckPermission("mall:customerCart:list")
    @GetMapping("/list")
    public TableDataInfo<CustomerCartVo> list(CustomerCartBo bo, PageQuery pageQuery) {
        return iCustomerCartService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出会员购物车列表
     */
    @SaCheckPermission("mall:customerCart:export")
    @Log(title = "会员购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CustomerCartBo bo, HttpServletResponse response) {
        List<CustomerCartVo> list = iCustomerCartService.queryList(bo);
        ExcelUtil.exportExcel(list, "会员购物车", CustomerCartVo.class, response);
    }

    /**
     * 获取会员购物车详细信息
     *
     * @param customerCartId 主键
     */
    @SaCheckPermission("mall:customerCart:query")
    @GetMapping("/{customerCartId}")
    public R<CustomerCartVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long customerCartId) {
        return R.ok(iCustomerCartService.queryById(customerCartId));
    }

    /**
     * 修改会员购物车
     */
    @SaCheckPermission("mall:customerCart:edit")
    @Log(title = "会员购物车", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CustomerCartBo bo) {
        return toAjax(iCustomerCartService.updateByBo(bo));
    }

    /**
     * 删除会员购物车
     *
     * @param customerCartIds 主键串
     */
    @SaIgnore
    @RequestMapping("/{customerCartIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] customerCartIds) {
        return toAjax(iCustomerCartService.deleteWithValidByIds(Arrays.asList(customerCartIds), true));
    }

    /**
     * 新增会员购物车
     */
    @SaIgnore
    @RepeatSubmit()
    @PostMapping("/addCartProduct")
    public R<Void> addCartProduct(@Validated(AddGroup.class) @RequestBody CustomerCartBo bo) {
        return toAjax(iCustomerCartService.insertByBo(bo));
    }

    /**
     * 查询用户购物车列表信息
     * @return
     */
    @SaIgnore
    @RequestMapping("/getCustomerCart")
    public R<List<CustomerCartVo>> getCustomerCart()
    {
        LoginUser loginUser = LoginHelper.getLoginUser();
        return R.ok(iCustomerCartService.queryCartByCustomerId(loginUser.getUserId()));
    }
}
